import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';

class Internet {
  //配置URL服务器地址
 final String urlStr="http://49.235.45.128:5000/";
 //final String urlStr="http://192.168.1.138:5000/";
  final int connectTimeout=5000;
  final int receiveTimeout=15000;
  
  Dio dio;

  //构建函数初始化
  Internet() {
    if(dio == null){
      initDio();
    }
  }

  //初始化Dio组件
  void initDio(){
    dio = Dio(BaseOptions(
      baseUrl: urlStr,
      headers: {'platform': 'android'},
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      responseType: ResponseType.plain,
    ));
    addHttpInterceptor(dio);
  }

  //post请求——返回Map类型数据
  Future<Map<String,dynamic>> post(String url,{dynamic params, dynamic options}) async {
    Response response;
    try{
      response = await dio.post(url, data: params, options: options);
    }catch(exception) {
      response=exception.response;
    }
    print("POST请求成功");
    print(response);
    if(response.data.runtimeType.toString() == "String"){
      return jsonDecode(response.data);
    }else{
      return response.data;
    }
  }

  //get请求——返回Map类型数据
  Future<Map<String,dynamic>> get(String url,{dynamic params}) async {
    Response response;
    try{
      response = await dio.get(url, queryParameters: params);
    }catch(exception) {
      response=exception.response;
    }
    print("GET请求成功");
    print(response);
    if(response.data.runtimeType.toString() == "String"){
      return jsonDecode(response.data);
    }else{
      return response.data;
    }
  }

  //设置一些HTTP请求报错问题
  DioError setHttpError(DioError e){
    DioError resMap;
    if(e.response == null){
      resMap=DioError(response: Response(data: {"flag":"failed","code":"","msg":"请检查网络连接","datas":false}));
    }else if(e.response.toString()==""){
      resMap=DioError(response: Response(data: {"flag":"failed","code":"","msg":"服务器无法响应","datas":false}));
    }else if(e.response.statusCode == 404){
      resMap=DioError(response: Response(data: {"flag":"failed","code":"","msg":"请求接口不存在","datas":false}));
    }else{
      resMap=e;
    }
    return resMap;
  }

  addHttpInterceptor(Dio dio) {
    dio.interceptors.add(InterceptorsWrapper(
      onRequest: (RequestOptions options) {
        //拦截请求之前设置通用请求参数
        Map<String,dynamic> other={
          "userid": DataSingleton().Data.id,
          "operaterUserId": DataSingleton().Data.id,
          "tenantId": DataSingleton().Data.tenantId,
          "warehouseid":DataSingleton().warehouseChoice.id
        };
        if(options.method.toString().toLowerCase() == "get"){
          options.queryParameters.addAll(other);
        }else if(options.method.toLowerCase() == "post"){
          options.data.addAll(other);
        }
        print(options.uri);
        print(options.queryParameters);
        print(options.data);
        return options; //continue
      }, 
      onResponse: (Response response) {
        return response; 
      }, 
      onError: (DioError e) {
        print("报错了");
        print(e);
        print(e.response);
        return setHttpError(e); 
      })
    );
  }
}
